package wire

type User struct {
	Id   string `json:"id"`
	Name string `json:"name"`
}

type UserService interface {
	GetUser(id string) *User
}

var _ UserService = (*userService)(nil)

type userService struct {
	mysql Database
	redis Database
}

func NewUserService(mysql Database, redis Database) UserService {
	return &userService{
		mysql: mysql,
		redis: redis,
	}
}

func (u *userService) GetUser(id string) *User {
	data := u.redis.GetById(id)
	if data == nil {
		data = u.mysql.GetById(id)
	}
	return toUser(data)
}

func toUser(data map[string]interface{}) *User {
	u := &User{
		Id:   data["id"].(string),
		Name: data["name"].(string),
	}

	return u
}
